package com.one.oa.api.service.impl;

import cn.hutool.core.util.RandomUtil;

import com.one.oa.api.bo.UserPhoneBo;
import com.one.oa.api.service.SmsService;
import com.one.oa.api.util.PhoneUtil;
import com.one.oa.constants.RedisKey;
import com.one.oa.dao.SmsDao;
import com.one.oa.entity.Sms;
import com.one.oa.type.SmsType;
import com.one.oa.util.AliSmsUtil;
import com.one.oa.vo.R;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**

 * @Description:
 * @Author：邢朋辉
 * @Date: 2023/3/17 11:16
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Resource
    private SmsDao dao;
    @Resource
    private StringRedisTemplate template;

    @Override
    public R sendRcode(String phone) {
        return sendCode(phone, AliSmsUtil.REGISTER, RedisKey.AUTH_ADDCODE,"注册", SmsType.注册验证码);
    }

    @Override
    public R checkRcode(UserPhoneBo bo) {
        //1.校验
        if(bo!=null && PhoneUtil.checkPhone(bo.getPhone())){
            //2.验证是否存在验证码
            if(template.hasKey(RedisKey.AUTH_ADDCODE+bo.getPhone())){
                //3.比较验证码
                if(bo.getCode()==Integer.parseInt(template.opsForValue().get(RedisKey.AUTH_ADDCODE+bo.getPhone()))){

                    //4.成功，删除验证码
                    template.delete(RedisKey.AUTH_ADDCODE+bo.getPhone());
                    //5.记录校验通过的手机号
                    template.opsForSet().add(RedisKey.AUTH_ADDPHONE,bo.getPhone());
                    //6.返回
                    return R.ok();
                }
            }
        }
        return R.fail("亲，无效哟！");
    }

    @Override
    public R all() {
        return R.ok(dao.selectList(null));
    }

    @Override
    public R sendLcode(String phone) {
       return sendCode(phone,AliSmsUtil.LOGIN,RedisKey.AUTH_LOGINCODE,"登录",SmsType.登录验证码);
    }

    @Override
    public R sendFcode(String phone) {
        return sendCode(phone,AliSmsUtil.FIND,RedisKey.AUTH_FINDCODE,"密码找回",SmsType.密码找回);
    }

    @Override
    public R checkFcode(UserPhoneBo bo) {
        //1.校验
        if(bo!=null && PhoneUtil.checkPhone(bo.getPhone())){
            //2.验证是否存在验证码
            if(template.hasKey(RedisKey.AUTH_FINDCODE+bo.getPhone())){
                //3.比较验证码
                if(bo.getCode()==Integer.parseInt(template.opsForValue().get(RedisKey.AUTH_FINDCODE+bo.getPhone()))){

                    //4.成功，删除验证码
                    template.delete(RedisKey.AUTH_FINDCODE+bo.getPhone());
                    //5.记录 当前的手机号
                    template.opsForSet().add(RedisKey.AUTH_FINDPHONE,bo.getPhone());

                    //6.返回
                    return R.ok();
                }
            }
        }
        return R.fail("亲，无效哟！");
    }

    /**
     * 发送验证码*/
    private R sendCode(String phone,String tem,String key,String msg,SmsType type){
        //1.校验
        if(PhoneUtil.checkPhone(phone)){
            //2.生成验证码
            int code= RandomUtil.randomInt(100000,999999);
            //3.发送短信 并校验是否成功
//            if(AliSmsUtil.sendMsg(tem,phone,code)){
            if(true){//模拟发送
                //4.发送成功,记录验证码
                //有效期  Redis String（每个验证码发送时间不一定一样）key 包含手机号 有效期 10分钟
                template.opsForValue().set(key +phone,code+"",10, TimeUnit.MINUTES);

                //5.记录发送日志
                dao.insert(new Sms(phone, type.getValue(), "实现"+msg+"验证码的发送"));
                //返回结果
                return R.ok(code);
            }
        }
        return R.fail("亲，手机号格式不正确！");
    }
}
